package com.net.wanjian.networkhospitalmanager.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.base.BaseActivity;
import com.net.wanjian.networkhospitalmanager.adapter.SignTimeAdapterX;
import com.net.wanjian.networkhospitalmanager.bean.SignResultBean;
import com.net.wanjian.networkhospitalmanager.bean.StudentApplyReserveReturn;
import com.net.wanjian.networkhospitalmanager.consts.ErrorCodeReturn;
import com.net.wanjian.networkhospitalmanager.net.HttpUtil;
import com.net.wanjian.networkhospitalmanager.netstatus.NetUtils;
import com.net.wanjian.networkhospitalmanager.utils.AntiShake;
import com.net.wanjian.networkhospitalmanager.utils.ProgressDialogUtils;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;
import com.net.wanjian.networkhospitalmanager.utils.ToastUtil;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;

import java.util.List;

import butterknife.BindView;
import io.reactivex.observers.DisposableObserver;

/**
 * Created by WJ on 2017/8/7.
 */

public class SignActivity extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.top_back_text_tv)
    TextView topBackTextTv;
    @BindView(R.id.top_back_layout)
    LinearLayout topBackLayout;
    @BindView(R.id.top_title_tv)
    TextView topTitleTv;
    @BindView(R.id.tv_Event)
    TextView tvEvent;
    @BindView(R.id.tv_room_name)
    TextView tvRoomName;
    @BindView(R.id.tv_teacher_name)
    TextView tvTeacherName;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_star_time)
    TextView tvStarTime;
    @BindView(R.id.btn_sign_in)
    Button btnSignIn;
    @BindView(R.id.btn_sign_out)
    Button btnSignOut;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.id_refresh)
    SwipeRefreshLayout idRefresh;
    private int qrCodeType;
    private int eventType;
    private String eventID;
    private SignTimeAdapterX signTimeAdapterX;
    private List<SignResultBean.DataBean.SignInOutRecordListBean> DataList;
    private String qrCode;
    private String event;
    private boolean SigNin = true;
    private int Signout;
    private SignResultBean.DataBean.SignInOutRecordListBean bean;

    @Override
    protected void initData() {

        SignResultBean.DataBean signResultBean = (SignResultBean.DataBean) getIntent().getSerializableExtra("signResultBean");
        qrCode = getIntent().getStringExtra("qrCodeType");
        qrCodeType = Integer.parseInt(qrCode);
        event = getIntent().getStringExtra("eventType");
        eventType = Integer.parseInt(event);
        eventID = getIntent().getStringExtra("eventID");

        String EventName = URLDecoderUtil.getDecoder(signResultBean.getEventInfo().getEventName());
        String EventTypeName = URLDecoderUtil.getDecoder(signResultBean.getEventInfo().getEventTypeName());
        String EventRoomName = URLDecoderUtil.getDecoder(signResultBean.getEventInfo().getEventRoomName());
        String TeacherName = URLDecoderUtil.getDecoder(signResultBean.getEventInfo().getEventTeacherName());
        String StartTime = URLDecoderUtil.getDecoder(signResultBean.getEventInfo().getEventStartTime());
        String eventEndTime = URLDecoderUtil.getDecoder(signResultBean.getEventInfo().getEventEndTime());
        tvEvent.setText(EventName);
        tvRoomName.setText(EventRoomName);
        tvTeacherName.setText(TeacherName);
        tvType.setText(EventTypeName);
        tvStarTime.setText(StartTime + " — " + eventEndTime);
        DataList = signResultBean.getSignInOutRecordList();
        idRefresh.setColorSchemeResources(R.color.color_ant, R.color.color_elephant, R.color.color_yuan);
        idRefresh.setOnRefreshListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        signTimeAdapterX = new SignTimeAdapterX(this);
        signTimeAdapterX.setList(DataList);
        recyclerView.setAdapter(signTimeAdapterX);
        btnSignIn.setOnClickListener(this);
        btnSignOut.setOnClickListener(this);
        topBackLayout.setOnClickListener(this);
        topTitleTv.setText("签到");


    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sign;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    public void onClick(View v) {
        AntiShake util = new AntiShake();
        switch (v.getId()) {
            case R.id.btn_sign_in:
                if (util.check()) return;
                if (DataList.isEmpty()) {
                    SignInX();
                    return;
                } else {
                    if (!DataList.get(DataList.size() - 1).getSignOutTime().isEmpty()) {
                        SignInX();
                    } else {
                        if (DataList.get(DataList.size() - 1).getSignInTime().size() > 0) {
                            ProgressDialogUtils.showAskDialog(SignActivity.this, "注意", "已有签到是否继续签到", "确定", "取消", new ProgressDialogUtils.OnClickDialogBtnListenner() {
                                @Override
                                public void onClickDialogBtn(int btnId) {
                                    if (btnId == 1) {
                                        SignInX();
                                    }
                                }
                            }, true);

                        }
                    }
                }

                break;
            case R.id.btn_sign_out:
                if (util.check()) return;
                if (!DataList.isEmpty()) {

                    Log.e(TAG, "onClick: " + DataList.get(DataList.size() - 1).getSignOutTime().size());
                    if (DataList.get(DataList.size() - 1).getSignOutTime().isEmpty()) {
                        ProgressDialogUtils.showAskDialog(SignActivity.this, "注意", "是否进行签退", "确定", "取消", new ProgressDialogUtils.OnClickDialogBtnListenner() {
                            @Override
                            public void onClickDialogBtn(int btnId) {
                                if (btnId == 1) {
                                    SignOutX();
                                }
                            }
                        }, true);
                    } else {
                        ToastUtil.showToast("请先进行签到，才能签退！");
                    }


                } else {
                    ToastUtil.showToast("请先进行签到，才能签退！");

                }


                break;
            case R.id.top_back_layout:
                finish();
                break;
        }
    }

    private void SignOutX() {
        HttpUtil.ScanSignInout(eventType, eventID, SharedXmlUtil.getInstance().getUserIdentityId(), 1, 0, qrCodeType, SharedXmlUtil.getInstance().getDeviceId(), SharedXmlUtil.getInstance().getToken(), SharedXmlUtil.getInstance().getHospitalId(), new DisposableObserver<StudentApplyReserveReturn>() {
            @Override
            public void onNext(StudentApplyReserveReturn studentApplyReserveReturn) {
                String result = studentApplyReserveReturn.getResult();
                if (result.equals("0")) {
                    ToastUtil.showToast("签退失败");
                    btnSignOut.setBackgroundResource(R.mipmap.logout);

                } else if (result.equals("1")) {
                    btnSignOut.setBackgroundResource(R.mipmap.signout);
                    RequestNetwork();

                } else if (result.equals("2005")) {
                    ToastUtil.showCenterToast("用户信息已被删除");

                } else if (result.equals("3002")) {
                    ToastUtil.showCenterToast("用户不在事件范围内");

                } else if (result.equals("3003")) {
                    ToastUtil.showCenterToast("事件不存在");

                } else if (result.equals("2004")) {
                    ToastUtil.showCenterToast("签到记录错误数据");

                }

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

    }

    private void SignInX() {
        HttpUtil.ScanSignInout(eventType, eventID, SharedXmlUtil.getInstance().getUserIdentityId(), 0, 0, qrCodeType, SharedXmlUtil.getInstance().getDeviceId(), SharedXmlUtil.getInstance().getToken(), SharedXmlUtil.getInstance().getHospitalId(), new DisposableObserver<StudentApplyReserveReturn>() {
            @Override
            public void onNext(StudentApplyReserveReturn studentApplyReserveReturn) {
                String result = studentApplyReserveReturn.getResult();
                if (result.equals("0")) {

                    ToastUtil.showToast("签到失败");
                } else if (result.equals("1")) {
                    btnSignOut.setBackgroundResource(R.mipmap.logout);
                    RequestNetwork();


                } else if (result.equals("2005")) {

                    ToastUtil.showCenterToast("用户信息已被删除");
                } else if (result.equals("3002")) {

                    ToastUtil.showCenterToast("用户不在事件范围内");
                } else if (result.equals("3003")) {

                    ToastUtil.showCenterToast("事件不存在");
                } else if (result.equals("2004")) {
                    ToastUtil.showCenterToast("签到记录错误数据");
                }

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

    }


    @Override
    public void onRefresh() {
        //清空数据源
        DataList.clear();
        //请求网络
        RequestNetwork();


    }

    private void RequestNetwork() {
        HttpUtil.ScanSign(qrCode, event, eventID, SharedXmlUtil.getInstance().getUserIdentityId(), SharedXmlUtil.getInstance().getDeviceId(), SharedXmlUtil.getInstance().getToken(), SharedXmlUtil.getInstance().getHospitalId(), new DisposableObserver<SignResultBean>() {

            @Override
            public void onNext(SignResultBean signResultBean) {
                if (signResultBean.getResult().equals(ErrorCodeReturn.success + "")) {
                    List<SignResultBean.DataBean.SignInOutRecordListBean> signInOutRecordList = signResultBean.getData().getSignInOutRecordList();
                    DataList.clear();
                    DataList.addAll(signInOutRecordList);
                    signTimeAdapterX.setList(DataList);
                    signTimeAdapterX.notifyDataSetChanged();
                    idRefresh.setRefreshing(false);
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

}
